<?php

/**
 * Cart Controller
 * @author Kartik
 * @category   Storefront
 * @package    Storefront_Controllers
 */
class Buyback_CartController extends Zend_Controller_Action
{
	protected $_cartModel;
	
	public function init()
	{
		$this->_cartModel = new Buyback_Model_Cart();
	}
	
	public function viewAction()
	{
		//all the code is take care of by the cart view helper.	
	}
	
	public function updateAction()
	{
		$request = $this->getRequest();
		if($request->isPost()){
			$qnty = $request->getPost();
			$qntyAsked = $qnty['qnty']['qnty'];
			$isbn = $qnty['qnty']['isbn'];
			$maxQnty = $this->_cartModel->offsetGet($isbn)->MaxQnty;
			$qntyToAdd = ($maxQnty <= $qntyAsked)? $maxQnty: $qntyAsked;
			if(0 <= $qntyToAdd && $maxQnty >= $qntyToAdd)
				$this->_cartModel->updateCartItem($isbn, $qntyToAdd);
							
		}
		
		$this->_helper->redirector->gotoUrl('/cart/view');
	}

	public function removeAction()
	{
		
		$request = $this->getRequest();
		if($request->isPost()){
			$qnty = $request->getPost();
			if(array_key_exists('rem',$qnty)){
				$this->_cartModel->removeItem($qnty['rem']['isbn']);
			}else{
				$this->_cartModel->removeItem();
			}
		}
		
		$this->_helper->redirector->gotoUrl('/cart/view');
	}
}